<?php
function lz_on_begin()
{
	//解析url 得到 id
	if (preg_match('/\?(\d{1,})/',$_SERVER['REQUEST_URI'],$matches))
	{
		$_GET['id'] = $matches[1];
	}
	if (LZ_MODULE == 'index')
	{
		if($_GET['cid'] && !$_GET['p']) 
			$_GET['p'] = 'photos';
		else if($_GET['id'] && !$_GET['p']) 
			$_GET['p'] = 'photo';
	}
	
}


function lz_on_after_db()
{
	global $db,$_time_start;
	include_once('model/cache.php');
	$cache = new LZ_Cache;
	if ($cache->exists())
	{
		header('content-type:text/html; charset=utf-8');
		echo $cache->get(false,$ttl);
		list($_usec, $_sec) = explode(' ', microtime()); 
		$_time_end = (float)$_usec + (float)$_sec;
		$lz_time_used = intval(($_time_end-$_time_start)*1000)/1000;
		echo '<!-- this is a cached page. time used: '.$lz_time_used.'s. this cache will expire in '.$ttl.' seconds -->';
		die;
	}
}


//刚刚进入系统，载入config 等文件之后，连接数据库之前
function lz_on_ready()
{
	//登录验证
	if (LZ_MODULE == 'admin')
	{
		if (!$_SESSION['login_user'])
		{
			header("location:login.php?module=".LZ_MODULE);
			die;
			//lz_exit(LOGIN_FIRST,'login.php?module=admin',1);
		}
	}

}

//载入了所有所需文件，并连接数据库之后
function lz_on_controller_begin()
{
	global $config,$db;


	//获取配置信息
	if (!is_array($config)) $config = array();
	$r = $db->query("SELECT * FROM `".LZ_MYSQL_PREFIX."config`;");
	while($arr = $db->fetch_array($r))
	{
		$arr['name'] && $config[$arr['name']] = $arr['value'];
	}
}

//解析模板以前
function lz_on_template_begin()
{
	global $all_view_data; //全局的模板数据
	$all_view_data['login_user'] = $_SESSION['login_user'];
	$all_view_data['now_time'] = date('H:i');
	$all_view_data['now_year'] = date('Y');
	$all_view_data['now_month'] = date('m');
	$all_view_data['now_day'] = date('d');
}

function lz_on_end()
{
	global $view_html;
	//if called cache_this_page() then cache this page ^_^
	if (LZ_CACHE_VIEW == 'yes' && defined('LZ_CACHE_TTL') && intval(LZ_CACHE_TTL) > 0)
	{
		$cache = new LZ_Cache;
		$cache->add( array('content'=>$view_html,'ttl'=>LZ_CACHE_TTL) );
	}
	echo $view_html;
}

